<?php
    /**
     * 收货地址类
     * Created by PhpStorm.
     * User: huangzhongwei
     * Date: 2018/10/24
     * Time: 14:21
     */

    namespace app\api\controller;

    use app\api\validate\AddressNew;
    use app\api\validate\DefaultAddress;
    use app\api\validate\IDMustBePositiveInt;
    use app\api\validate\ReceiverAddress;
    use app\common\model\HarvestAddress as HarvestAddressModel;
    use app\lib\exception\MissException;
    use app\lib\exception\SuccessMessage;
    use app\api\service\Token;

    class HarvestAddress extends BaseController
    {

        protected $beforeActionList = [
            'checkExclusiveScope',
        ];

        /**新增收货地址
         * @return SuccessMessage
         * @throws \app\lib\exception\ParameterException
         * @throws \think\db\exception\DataNotFoundException
         * @throws \think\db\exception\ModelNotFoundException
         * @throws \think\exception\DbException
         */
        public function create()
        {
            (new AddressNew())->goCheck();
            $data = input('post.');
            $uid  = Token::getCurrentUid();
            //判断当前用户是否存在收货地址
            $address                 = HarvestAddressModel::where('account_id', $uid)
                                                          ->where('university_id', $data['university_id'])
                                                          ->where('type', $data['type'])
                                                          ->find();
            $data['default_address'] = !$address ? 1 : 0;
            $data['account_id']      = $uid;
            (new HarvestAddressModel())->save($data);
            return new SuccessMessage();
        }

        /**取货地址
         * @return SuccessMessage
         * @throws \app\lib\exception\ParameterException
         * @throws \think\db\exception\DataNotFoundException
         * @throws \think\db\exception\ModelNotFoundException
         * @throws \think\exception\DbException
         */
        public function createReceiver()
        {
            (new ReceiverAddress())->goCheck();
            $data = input('post.');
            $uid  = Token::getCurrentUid();
            //判断当前用户是否存在取货地址
            $address                 = HarvestAddressModel::where('account_id', $uid)
                                                          ->where('university_id', $data['university_id'])
                                                          ->where('type', 2)
                                                          ->find();
            $data['type']            = 2;
            $data['default_address'] = !$address ? 1 : 0;
            (new HarvestAddressModel())->save($data);
            return new SuccessMessage();
        }


        /**
         * @param $university_id 大学id
         * @param $type          1:收货地址 2 取货地址
         *
         * @return array|\PDOStatement|string|\think\Collection
         * @throws \app\lib\exception\ParameterException
         */
        public function getAddress($university_id, $type)
        {
            $uid = Token::getCurrentUid();

            $address = HarvestAddressModel::getAddressByUniversityId($uid, $type, $university_id);

            return $address;
        }

        /**获取指定id收货地址
         *
         * @param $id
         *
         * @return HarvestAddressModel|MissException|null
         * @throws \app\lib\exception\ParameterException
         * @throws \think\Exception\DbException
         */
        public function getAddressById($id)
        {
            (new IDMustBePositiveInt())->goCheck();
            $address = HarvestAddressModel::with('university')->get($id);
            if (!$address) {
                return new MissException(['msg' => '当前地址不存在', 'errorCode' => '40000']);
            }
            return $address;
        }


        /**
         * 收货地址修改
         * @return SuccessMessage
         * @throws MissException
         * @throws \app\lib\exception\ParameterException
         * @throws \think\db\exception\DataNotFoundException
         * @throws \think\db\exception\ModelNotFoundException
         * @throws \think\exception\DbException
         */
        public function save()
        {
            (new IDMustBePositiveInt())->goCheck();
            (new AddressNew())->goCheck();
            $data = input('post.');
            //判断当前收货地址是否存在
            $address = HarvestAddressModel::where('id', $data['id'])->find();
            if (!$address) {
                throw new MissException(['msg' => '当前收货地址不存在', 'errorCode' => '40000']);
            }
            $address->save($data);
            return new SuccessMessage();
        }

        /**
         * 修改取货地址
         * @return SuccessMessage
         * @throws MissException
         * @throws \app\lib\exception\ParameterException
         * @throws \think\db\exception\DataNotFoundException
         * @throws \think\db\exception\ModelNotFoundException
         * @throws \think\exception\DbException
         */
        public function saveReceiver()
        {
            (new IDMustBePositiveInt())->goCheck();
            (new ReceiverAddress())->goCheck();
            $data = input('post.');
            //判断当前收货地址是否存在
            $address = HarvestAddressModel::where('id', $data['id'])->find();
            if (!$address) {
                throw new MissException(['msg' => '当前取货地址不存在', 'errorCode' => '40000']);
            }
            $address->save($data);
            return new SuccessMessage();
        }

        /**
         * 修改默认地址
         * @return SuccessMessage
         * @throws MissException
         * @throws \app\lib\exception\ParameterException
         * @throws \think\db\exception\DataNotFoundException
         * @throws \think\db\exception\ModelNotFoundException
         * @throws \think\exception\DbException
         */
        public function editDefault()
        {
            (new DefaultAddress())->goCheck();
            $data = input('post.');

            $uid = Token::getCurrentUid();
            //修改原先默认地址
            $defaultAddress = HarvestAddressModel::getDefaultAddress($uid, $data['university_id'], $data['type']);

            if ($defaultAddress) {
                $defaultAddress->default_address = 0;
                $defaultAddress->save();
            }

            $address = HarvestAddressModel::where(['id' => $data['id']])
                                          ->find();

            if (!$address) {
                throw new MissException(['msg' => '当前地址不存在', 'errorCode' => '40000']);
            }

            $address->default_address = 1;
            $address->save();

            return new SuccessMessage();
        }

        /**
         * 获取当前默认地址
         *
         * @param $id 大学id
         *
         *
         * @return mixed
         *
         * @throws MissException
         * @throws \app\lib\exception\ParameterException
         */
        public function getDefaultAddress($id, $type)
        {
            (new IDMustBePositiveInt())->goCheck();
            $uid            = Token::getCurrentUid();
            $defaultAddress = HarvestAddressModel::getDefaultAddress($uid, $id, $type);
            if (!$defaultAddress) {
                throw new MissException(['msg' => '当前默认地址不存在', 'errorCode' => '40000']);
            }

            return $defaultAddress;
        }


    }